<?php
	//	数据库基类
	class DBo{
		private $host='';
		private $port;
		private $user;
		private $pass;
		private $dbname;
		private $charset;
		private $link;
		//
		private $prefix;
		
		//	构造函数初始化属性值
		public function __construct($arr=array()){
			$this->host=isset($arr['host'])?$arr['host']:$GLOBALS['config']['mysql']['host'];
			$this->port=isset($arr['port'])?$arr['port']:$GLOBALS['config']['mysql']['port'];
			$this->user=isset($arr['user'])?$arr['user']:$GLOBALS['config']['mysql']['user'];
			$this->pass=isset($arr['pass'])?$arr['pass']:$GLOBALS['config']['mysql']['pass'];
			$this->dbname=isset($arr['dbname'])?$arr['dbname']:$GLOBALS['config']['mysql']['dbname'];
			$this->charset=isset($arr['charset'])?$arr['charset']:$GLOBALS['config']['mysql']['charset'];
			$this->prefix=isset($arr['prefix'])?$arr['prefix']:$GLOBALS['config']['mysql']['prefix'];
			
			$this->initial();
		}
		//数据库其它初始化方法
		private function initial(){
			$this->db_link();
			$this->db_charset();
			$this->db_use();
		}

		
		//连接数据库方法
		private function db_link(){
			@$this->link = mysql_connect($this->host.":".$this->port,$this->user,$this->pass);
			if(!$this->link){
				echo "数据库连接失败：<br />";
				echo "错误编号：".mysql_errno()."<br />";
				echo "错误原因：".mysql_error()."<br />";
				exit;
			}
		}
		//提交sql语句方法
        private function  db_query($sql){
			$res = mysql_query($sql);
			if(!$res){
				echo "{$sql}语句有误：<br />";
				echo "错误编号：".mysql_errno()."<br />";
				echo "错误原因：".mysql_error()."<br />";			
				exit;
			}
			return $res;
		}
		//设置字符集方法
        /**
         *
         */
        private function db_charset(){
			$sql = "set names {$this->charset}";
			$this->db_query($sql);
		}

		//选择数据库方法
		private function db_use(){
			$sql="use {$this->dbname}";
			$this->db_query($sql);
		}
		//添加数据库方法
        /**
         * @param $sql
         * @return int
         */
        protected function Insert($sql){
			$res = $this->db_query($sql);
			return mysql_insert_id(); 
		}

        //更新数据库方法,返回影响行数
		protected function Update($sql){
			 $this->db_query($sql);
			return mysql_affected_rows(); 
		}
        /**
         *删除一条或多条数据
         * @param $sql
         * @return int:受影响行数
         */
        protected function Delete($sql){
			 $this->db_query($sql);
			return mysql_affected_rows(); 
		}

        /**
         * @param $sql
         * @return array_row or false
         */
        protected function queryRow($sql){
			//调用SQL执行函数
			$res = $this->db_query($sql);

			//从结果集中提取数据
			return mysql_fetch_assoc($res);
		}

        /**
         * @param $sql
         * @return array_list or array()
         */
        protected function queryAll($sql){
			$res = $this->db_query($sql);
			$list = array();
			while($row =mysql_fetch_assoc($res)){
                $list[]=$row;
			}
			return $list;
		}

		public function __sleep(){
			//link属性是资源，不需要保存
			return array('host','port','user','pwd','dbname','charset','prefix');
		}
		
		public function __wakeup(){
				$this->initial();
		}

		/*
		 * 构造表名
		 * @param1 string $table = ''，要获取全名的表的名字
		 * @return 带前缀的表全名
		*/
		protected function getTable($table = ''){
			return ($table == '') ?  $this->prefix . $this->table : $this->prefix . $table;
			//父类的表前缀拼凑子类的表名属性
		}

        /**
         * 查询一下表中所有记录数
         * @param $table_name 表名不用加前缀
         */
        public function getCount($table_name=''){
            $sql = "select count(*) as c from {$this->getTable($table_name)}";
            $count = $this->queryRow($sql);
            return $count['c'];
        }
	}